/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.myfaces.extensions.cdi.forge.example;

import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.project.Project;
import org.jboss.forge.project.dependencies.events.AddedDependencies;
import org.jboss.forge.project.facets.BaseFacet;
import org.jboss.forge.project.facets.DependencyFacet;
import org.jboss.forge.project.facets.WebResourceFacet;
import org.jboss.forge.project.facets.events.InstallFacets;
import org.jboss.forge.resources.FileResource;
import org.jboss.forge.resources.Resource;
import org.jboss.forge.scaffold.AccessStrategy;
import org.jboss.forge.scaffold.ScaffoldProvider;
import org.jboss.forge.scaffold.util.ScaffoldUtil;
import org.jboss.forge.shell.ShellPrintWriter;
import org.jboss.forge.shell.ShellPrompt;
import org.jboss.forge.shell.plugins.Alias;
import org.jboss.forge.shell.plugins.RequiresFacet;
import org.jboss.forge.spec.javaee.CDIFacet;
import org.jboss.forge.spec.javaee.FacesFacet;
import org.jboss.forge.spec.javaee.PersistenceFacet;
import org.jboss.seam.render.TemplateCompiler;
import org.metawidget.forge.MetawidgetScaffold;

import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Cosmin Martinconi, 12.08.2011
 */
@Alias("codiExample")
@RequiresFacet({WebResourceFacet.class,
        DependencyFacet.class,
        PersistenceFacet.class,
        CDIFacet.class,
        FacesFacet.class})
public class CodiExample extends BaseFacet implements ScaffoldProvider {

    private ShellPrompt prompt;

    private MetawidgetScaffold metawidgetScaffold;

    @Inject
    public CodiExample(ShellPrompt prompt, ShellPrintWriter writer, TemplateCompiler compiler, Event<InstallFacets> install) {
        metawidgetScaffold = new MetawidgetScaffold(prompt, writer, compiler, install);
    }

    @Override
    public void setProject(Project project) {
        super.setProject(project);
        metawidgetScaffold.setProject(project);
    }

    public List<Resource<?>> setup(final boolean overwrite) {
        List<Resource<?>> resources = metawidgetScaffold.setup(overwrite);
        resources.addAll(generateExampleResources(overwrite));

        return resources;
    }

    public void handleAddedDependencies(@Observes final AddedDependencies event) {
        metawidgetScaffold.handleAddedDependencies(event);
    }

    public List<Resource<?>> generateFromEntity(final JavaClass entity, final boolean overwrite) {
        return metawidgetScaffold.generateFromEntity(entity, overwrite);
    }

    public void createMetawidgetConfig(final boolean overwrite) {
        metawidgetScaffold.createMetawidgetConfig(overwrite);
    }

    public void createPersistenceUtils(final boolean overwrite) {
        metawidgetScaffold.createPersistenceUtils(overwrite);
    }

    public void createFacesUtils(final boolean overwrite) {
        metawidgetScaffold.createFacesUtils(overwrite);
    }

    @SuppressWarnings("unchecked")
    public boolean install() {
        System.out.println("CodiExample is using MetawidgetScaffold");
        return metawidgetScaffold.install();
    }

    public boolean isInstalled() {
        return metawidgetScaffold.isInstalled();
    }

    public List<Resource<?>> generateIndex(final boolean overwrite) {
        return metawidgetScaffold.generateIndex(overwrite);
    }

    public List<Resource<?>> getGeneratedResources() {
        return metawidgetScaffold.getGeneratedResources();
    }

    public AccessStrategy getAccessStrategy() {
        return metawidgetScaffold.getAccessStrategy();
    }

    public List<Resource<?>> generateTemplates(final boolean overwrite) {
        return metawidgetScaffold.generateTemplates(overwrite);
    }

    private List<Resource<?>> generateExampleResources(final boolean overwrite) {

        List<Resource<?>> result = new ArrayList<Resource<?>>();
        WebResourceFacet web = project.getFacet(WebResourceFacet.class);

        result.add(ScaffoldUtil.createOrOverwrite(prompt, web.getWebResource("/layout/main-template.xhtml"),
                getClass().getResourceAsStream("/org/apache/myfaces/extensions/cdi/forge/web/main-template.xhtml"), overwrite));

        result.add(ScaffoldUtil.createOrOverwrite(prompt, web.getWebResource("/layout/menu.xhtml"),
                getClass().getResourceAsStream("/org/apache/myfaces/extensions/cdi/forge/web/menu.xhtml"), overwrite));

        FileResource<?> codiLogo = project.getFacet(WebResourceFacet.class).getWebResource("/resources/images/codi_logo.png");
        if (!codiLogo.exists()) {
            codiLogo.setContents(getClass().getResourceAsStream("/org/apache/myfaces/extensions/cdi/forge/web/resources/images/codi_logo.png"));
        }

        FileResource<?> MFLogo = project.getFacet(WebResourceFacet.class).getWebResource("/resources/images/myfaces_logo.png");
        if (!MFLogo.exists()) {
            MFLogo.setContents(getClass().getResourceAsStream("/org/apache/myfaces/extensions/cdi/forge/web/resources/images/myfaces_logo.jpg"));
        }

        return result;
    }

}
